/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package byzantine.views;

import byzantine.ByzantineModel;
import byzantine.Message;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;

/**
 *
 * @author Ryhan
 */
public class SentMessageView extends JPanel implements View {

    private static final String sentMessagesString = "Sent Messages";

    //private int width;
    //private int height;

    private int width = 250;
    private int height = 150;

    private ByzantineModel model;
    private JLabel panelName;
    private JTextPane samplePane;
    private JScrollPane scrollPane;

    public SentMessageView(ByzantineModel model) {
        this.model = model;
        model.addObserver(this);
/*
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension screenSize = t.getScreenSize();
        width = (int)((screenSize.width - 10) * 0.185);
        height = (int)((screenSize.height - 100) * 0.19);
*/
        initializeView();
    }

    private void initializeView() {

        // Initialize the components
        panelName = new JLabel(sentMessagesString);

        samplePane = new JTextPane();
        samplePane.setFont(new Font("Monospaced", Font.PLAIN, 12));
        samplePane.setEditable(false);

        scrollPane = new JScrollPane(samplePane);
        scrollPane.setPreferredSize(new Dimension(width, (int) (height * .8)));
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setWheelScrollingEnabled(true);

        this.setBackground(Color.LIGHT_GRAY);
        this.add(panelName);
        this.add(scrollPane);

        setBorder(BorderFactory.createEtchedBorder());
    }

    private void populateMessages() {
        StyledDocument doc = samplePane.getStyledDocument();
        if (model.isSimulationRunning() 
                && model.getExaminedStep() < model.getNumRounds()) {
            Integer step = model.getExaminedStep();
            if (step < model.getNumRounds()) {
                panelName.setText(sentMessagesString);
                try {
                    doc.remove(0, doc.getLength());

                    ArrayList<Message> sentMessages = model.getSelectedGeneral().getSentMessages(step);
                    Integer messageCount = model.getNumGenerals() - 1;
                    String messageCountString = messageCount.toString();
                    for (int i = 0; i < sentMessages.size() / messageCount; i++) {
                        doc.insertString(doc.getLength(), sentMessages.get(i * messageCount).toString() + "  x" + messageCountString + "\n", null);
                    }
                    samplePane.setCaretPosition(0);
                } catch (Exception e) {
                    //e.printStackTrace(System.err);
                }
            }
        } else {
            try {
                doc.remove(0, doc.getLength());
            } catch (Exception e) {
                //e.printStackTrace(System.err);
            }
        }
    }

    public void valueChanged() {
        if (model.hasSimulationStatusChanged()
                || model.hasSelectedGeneralChanged()
                || model.hasExaminedStepChanged()) {
            populateMessages();
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);

    }
}
